package com.kmvc.message.activity;

import java.io.File;
import java.util.Date;

import com.kmvc.message.lock.LockMainActivity;
import com.kmvc.message.R;
import com.kmvc.message.tools.NetWorkTool;
import com.kmvc.message.service.DownloadService;
import com.kmvc.message.tools.WebService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.telephony.TelephonyManager;

public class SplashActivity extends Activity 
{
	public String tel = "";
	public String IMSI = "";
	public String MolNo = "";
	private String CheckResult = "nodata";
	public int netversion;
	public int localVerCode;
	private Handler updatehandler = new Handler();
	private Handler checkbindhandler = new Handler();
	
	
	
	
	Runnable runnableupdate = new Runnable() {
		public void run() {
			try {
				localVerCode = getPackageManager().getPackageInfo(
						"com.kmvc.message", 0).versionCode;
				SharedPreferences shp = getSharedPreferences("phonenumber", 0);
				Editor editor = shp.edit();
				editor.putString("tel", tel);
				editor.commit();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (localVerCode < netversion) {
				Intent intent = new Intent();
				intent.setClass(SplashActivity.this, DownloadService.class);
				intent.putExtra("PNAME", "Message.apk");
				intent.putExtra("URL",
						"http://222.221.20.141/qrcode/apk/Message.apk");
				intent.putExtra("NAME", "信息通报");
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				SplashActivity.this.startService(intent);
			}
		}
	};
	
	Runnable runnableCB = new Runnable() {
		public void run() {
			//progress.dismiss();
			// Log.e("MainActivitynew", CheckResult);

			if (CheckResult.equals("") || CheckResult.equals("NO")|| CheckResult.equals("anyType{}")) 
			{
				showInputDialog();
			} else 
			{
				curDateBefor = new Date(System.currentTimeMillis());//获取当前时间
				long diff = curDateBefor.getTime() - curDate.getTime();
				if(5000-diff>0)
				{
					try 
					{
						this.wait(5000-diff);
					} 
					catch (Exception e) 
					{
						e.printStackTrace();
					}
					
				}
				
				
				SharedPreferences shp = getSharedPreferences("phonenumber", 0);
				if(CheckResult!=null&&!CheckResult.equals(""))
				{
					Editor editor = shp.edit();
					editor.putString("phone", CheckResult);
					editor.commit();
				}
				
				Intent inte = new Intent(SplashActivity.this,LockMainActivity.class);
				inte.putExtra("PhoneNumber", CheckResult);
				SplashActivity.this.startActivity(inte);
				SplashActivity.this.finish();
			}
		}
	};
	
	
	Date curDate;
	Date curDateBefor;
	protected void onCreate(Bundle paramBundle)
	{
		super.onCreate(paramBundle);
		setContentView(R.layout.splash);	
	}
	
	
	protected void onResume()
	{
		super.onResume();
		curDate = new Date(System.currentTimeMillis());//获取当前时间
		DeleteShoot();
		try 
		{
			CheckUpdate();
		} 
		catch (Exception e) 
		{
			e.toString();
		}
		
		try 
		{
			TelephonyManager mTelephonyMgr = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
			IMSI = mTelephonyMgr.getSubscriberId()+"";
			MolNo = mTelephonyMgr.getDeviceId()+"";
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		SharedPreferences shp = getSharedPreferences("phonenumber", 0);
		SharedPreferences shp1 = getSharedPreferences("lock_pwd", 0);
		Editor editor = shp.edit();
		editor.putString("IMSI", IMSI);
		editor.putString("MolNo", MolNo);
		editor.commit();
		if (IMSI.equals("")||IMSI.equals("null")) 
		{
			new AlertDialog.Builder(SplashActivity.this)
					.setMessage("抱歉,您的手机不能使用此软件")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									System.exit(0);
								}
							}).show().setCancelable(false);
		}
		else 
		{
			Intent intt = getIntent();
			String LastAc = "";
			try {
				LastAc = "" + intt.getStringExtra("LastAc");
			} catch (Exception e) {
				e.toString();
				LastAc = "NO";
			}
			if (LastAc.equals("Lock")||shp1.getString("lock_pwd", "").equals("")) 
			{
				showInputDialog();
			} 
			else 
			{
				//progress = ProgressDialog.show(this, "请稍候", "读取数据中...", true);
				CheckBind();
			}
		}
	}
	
	
	
	private void DeleteShoot() {
		// 
		try {
			File destDir = new File(Environment.getExternalStorageDirectory()
					.toString() + "/KMVC_MESSAGE");
			if (destDir.exists()) {
				if (destDir.isDirectory()) {
					File[] childFiles = destDir.listFiles();
					for (int i = 0; i < childFiles.length; i++) {
						if (childFiles[i].isFile()&& childFiles[i].getName().endsWith(".png")) 
						{
							childFiles[i].delete();
						}
					}
				}
			}
		} catch (Exception e) 
		{
			// 
		}

	}
	
	
	/*
	 * 检查更新
	 */
	private void CheckUpdate() 
	{
		new Thread() {
			public void run() {
				String temp = "0";
				try 
				{
					temp = NetWorkTool.getContent("http://222.221.20.141/qrcode/MessageAndroid.txt");
					tel = NetWorkTool.getContent("http://222.221.20.141/qrcode/txt/tell.txt");
					netversion = Integer.parseInt(temp);
				} catch (Exception e) {
					e.printStackTrace();
					temp = "0";
				}
				try {
					netversion = Integer.parseInt(temp);
				} catch (Exception e) {
					netversion = 0;
				}
				updatehandler.post(runnableupdate);
			}
		}.start();

	}
	
	
	private void CheckBind() {

		new Thread() {
			public void run() {
				try {
					CheckResult = "" + WebService.CheckBind(IMSI, MolNo);
				} catch (Exception e) {
					e.printStackTrace();
					CheckResult = "";
				}

				checkbindhandler.post(runnableCB);
			}
		}.start();
	}
	
	protected void showInputDialog() 
	{
		Intent intent=new Intent(this,LoginActivity.class);
		startActivity(intent);
		this.finish();
	}
	
	

}
